/**
 * <p>cceafguidepanel的控制类</p>
 * 继承自Ext.app.Controller

 * @module Ext-ux-cceaf-controller
 * @class cceaf.controller.GuidePanelCtrl
 * @extends Ext.app.Controller
 * @since 1.0
 */
Ext.define('cceaf.controller.GuidePanelCtrl',{
	extend: 'Ext.app.Controller', 
	requires: [
	    
    ],
	
    constructor: function(config){
		config = config || {};
		
		config = Ext.applyIf(config || {}, {

		});

		cceaf.controller.GuidePanelCtrl.superclass.constructor.apply(this, arguments);
	},

	/**
     * 监听事件
     */
    init: function() {
    	//控制层需控制视图的xtype
    	var controlledViewXType = this.controlledViewXType;
    	if (!controlledViewXType) {
    		//控制名称
    		var className = this.self.getName();
    		alert('必须设置控制层' + className + '需控制视图的xtype，即controlledViewXType属性！');
    		return;
    	}
    	
    	//构建初始化的事件监听
    	var eventTemplate = "this.control({" + 
	    	"'aaaaa': {" +
	    	"	afterrender: this.initPage" +
	        "}," +
        	"'aaaaa #btnPrev': {" + 
            "    click: this.page_prev" + 
            "}," + 
            "'aaaaa #btnNext': {" + 
            "    click: this.page_next" + 
            "}," + 
            "'aaaaa #btnClose': {" + 
            "    click: this.page_close" + 
            "}" + 
    	"});";
	    
	    var eventString = eventTemplate.replace(/aaaaa/gi, controlledViewXType);
	    eval(eventString);
        
        this.registerEvent();
        
      //标记已经初始化完成
        this.isInit = true;
    },
    
    /**
     * 对于动态加载controller模式
     * 第一次加载后设置为true，再次加载后不在执行init方法 
     * 
     * @property isInit 
     * @type Boolean
     * @default false
     */
    isInit: false,
    
    /**
     * 监听其他事件
     */
    registerEvent: function(){
    	
    },
    
    /**
	 * 关闭弹出页面
	 *
     * @method page_close 
     */
    page_close: function(){
    	var billForm = this.getControlledView();
    	var addAndEditWin = billForm.up('window');
    	if (addAndEditWin) {
    		addAndEditWin.close();
    	}
    },
    
    /**
	 * 获取视图
	 *
     * @return {Ext.container.Container} 视图对象
     */
    getControlledView: function(){
    	var controlledViewXType = this.controlledViewXType;
    	var methodName = 'this.get' + controlledViewXType.substr(0,1).toUpperCase() + controlledViewXType.substring(1) + '()';
    	
    	var view = eval(methodName);
    	return view;
    },
    
    /**
     * 进入页面时，设置初始值
     */
    initPage: function(){
    	
    },
    
    /**
     * 上一步
     */
    page_prev: function() {
    	this.cardNav(-1);
    },
    /**
     * 下一步
     */
    page_next: function() {
    	this.cardNav(1);
    },
    
    /**
     * 导航
     * direction： -1 上一步
     * direction： 1 下一步
     * 
     * @param {number} direction 导航方向
     */
    cardNav: function(direction) {
    	var billForm = this.getControlledView();
    	var stepNumber = billForm.stepNumber;
    	var activeItem = billForm.activeItem;
        if (direction == -1) {
        	activeItem--;
            if (activeItem < 0) { 
            	activeItem = 0; 
            	return;
            }
        }
        
        if (direction == 1) {
        	//执行action
        	if (!this.stepAction(activeItem)) {
        		return;
        	}
        	
        	activeItem++;
            if (activeItem > stepNumber-1) { 
            	activeItem = stepNumber-1; 
            	return;
            } 
        }

        var btnPrev = billForm.down("#btnPrev");
        var btnNext = billForm.down("#btnNext");

        var btnPrevDisabled;
		var btnNextDisabled;
		var btnNextText;
		var btnNextIconCls;
		if (stepNumber == 1) {
			btnPrevDisabled = true;
			btnNextDisabled = false;
			btnNextText = " 完成 ";
			btnNextIconCls = "completeIcon";
		} else {//stepNumber>1
			if (activeItem == 0) {
				btnPrevDisabled = true;
				btnNextDisabled = false;
				btnNextText = " 下一步 ";
				btnNextIconCls = "nextIcon";
			} else {
				btnPrevDisabled = false;
				btnNextDisabled = false;
				if (activeItem == stepNumber - 1) {
					btnNextText = " 完成 ";
					btnNextIconCls = "completeIcon";
				} else {
					btnNextText = " 下一步 ";
					btnNextIconCls = "nextIcon";
				}
			}
		}
		
		btnPrev.setDisabled (btnPrevDisabled);
		btnNext.setDisabled (btnNextDisabled);
		btnNext.setText (btnNextText);
		btnNext.setIconCls (btnNextIconCls);

		billForm.activeItem = activeItem;
        billForm.getLayout().setActiveItem(activeItem);
        
//        if(direction == 1){
//        	this.initAction(activeItem-1);
//        }
    },
    
    /**
     * 执行向导动作,可在子类重载
     * 
     * @param {number} activeItem 活动的item索引
     */
    stepAction: function(activeItem){
    	var action = "this.step" + activeItem + "Action";
    	var actionFunction = eval(action);
    	
    	if (Ext.isFunction(actionFunction)) {
    		return eval(action + "()");
    	}
    	
    	return true;
    },
    
    
    /**
     * 检查条件,给下个页面设置初始默认值,可控制按钮是否可用
     * 
     * @param {number} activeItem 活动的item索引
     */
    initAction:function(activeItem){
    	var action = "this.init" + activeItem + "Action";
    	var actionFunction = eval(action);
    	if (Ext.isFunction(actionFunction)) {
    		eval(action + "()");
    	}
    }
    
});